<?php

namespace app\data\controller;

use app\data\model\LiveAppList;
use think\admin\Controller;
use think\admin\model\SystemUser;
use think\Session;

/**
 * 应用列表
 * Class AppListController
 * @package app\customer\controller
 */
class AppListController extends Controller
{
    /**
     * 查询
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function index()
    {
        $this->title = '应用列表';
        $this->users = SystemUser::mk()->field('id,nickname,username')->select()->toArray();

        $this->type = $this->request->get('type', 'index');
        $query = LiveAppList::mQuery();
        if ($this->type === 'index') $query->where(['deleted' => 0]);
        elseif ($this->type === 'recycle') $query->where(['deleted' => 1]);
        else $this->error("无法加载 {$this->type} 数据列表！");

        $query->like('app_name');
        $query->equal('system_userid,create_userid');
        $query->dateBetween('create_time');
        $query->order('create_time desc')->page();
    }
    /**
     * 新增
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function add()
    {
        LiveAppList::mForm('form', 'app_id');
    }

    /**
     * 新增表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _add_form_filter(array &$data)
    {
        if($this->request->isGet()){
            $this->users = SystemUser::mk()->field('id,nickname')->where(['is_deleted'=>0,'status'=>1])->select()->toArray();
        }
        if ($this->request->isPost()) {
            if(empty($data['system_userid'])) $this->error('请选择所属人！');
            LiveAppList::mk()->insert([
                'app_name'          => $data['app_name'],
                'app_package'       => $data['app_package'],
                'update_message'    => $data['update_message'],
                'update_versioncode'=> $data['update_versioncode'],
                'system_userid'     => $data['system_userid'],
                'app_logo'          => $data['app_logo'],
                'app_kefu_qrcode'   => $data['app_kefu_qrcode'],
                'app_kefu_msg'      => $data['app_kefu_msg'],
                'update_downloadurl'=> $data['update_downloadurl'],
                'create_userid'     => Session('user')['id'],
                'remarks'           => $data['remarks'],
            ]);
            $this->success('新增成功！');
        }
    }

    /**
     * 编辑
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function edit()
    {
        LiveAppList::mForm('form', 'app_id');
    }

    /**
     * 编辑表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _edit_form_filter(array &$data)
    {
        if($this->request->isGet()){
            $this->users = SystemUser::mk()->field('id,nickname')->where(['is_deleted'=>0,'status'=>1])->select()->toArray();
        }
        if ($this->request->isPost()) {
            if(empty($data['system_userid'])) $this->error('请选择所属人！');
            //更新内容
            LiveAppList::mk()->where(['app_id' => $data['app_id']])->update([
                'app_name'         => $data['app_name'],
                'app_package'      => $data['app_package'],
                'update_message'    => $data['update_message'],
                'update_versioncode'=> $data['update_versioncode'],
                'system_userid'    => $data['system_userid'],
                'app_logo'         => $data['app_logo'],
                'app_kefu_qrcode'  => $data['app_kefu_qrcode'],
                'app_kefu_msg'      => $data['app_kefu_msg'],
                'update_downloadurl'=> $data['update_downloadurl'],
                'create_userid'     => Session('user')['id'],
                'remarks'           => $data['remarks'],
            ]);
            $this->success('编辑成功！');
        }
    }
    /**
     * 删除
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function remove()
    {
        LiveAppList::mSave($this->_vali([
            'deleted.in:0,1' => '状态值范围异常！',
            'deleted.require' => '状态值不能为空！',
        ]), 'app_id');
    }
}